//---------------------------------------------------------------------
// This file is part of the Microsoft BizTalk Server 2006 SDK
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.BizTalk.Wizard;
using System.Diagnostics;
using System.Reflection;
using VSLangProj;

namespace MsPartners.Unisys.BizTalkAdapters.AdapterWizard
{
	public class AdapterWizardForm : Microsoft.BizTalk.Wizard.WizardForm
	{
		private enum Pages
		{
			wzPageWelcome1 = 0,
			wzPageGeneralSetup1 = 1,
			wzPageReceiveSetup1 = 2,
			wzPageReceiveProperties1 = 3,
			wzPageTransmitSetup1 = 4,
			wzPageTransmitProperties1 = 5,
			wzPageCodeProperties1 = 6,
			wzPageSummary1 = 7
		}

		private Hashtable wizardResults = new Hashtable();
		private List<AdapterProperty> transmitHandlerProperties = new List<AdapterProperty>();
		private List<AdapterProperty> transmitEndpointProperties = new List<AdapterProperty>();
		private List<AdapterProperty> receiveHandlerProperties = new List<AdapterProperty>();
		private List<AdapterProperty> receiveEndpointProperties = new List<AdapterProperty>();

		private ArrayList pageCollection = new ArrayList();
		private int pageCount = 0;
		private WzPageCodeProperties wzPageCodeProperties1;
		private WzPageGeneralSetup wzPageGeneralSetup1;
		private WzPageSummary wzPageSummary1;
		private WzPageTransmitSetup wzPageTransmitSetup1;
		private WzPageTransmitProperties wzPageTransmitProperties1;
		private WzPageWelcome wzPageWelcome1;
		private WzPageReceiveSetup wzPageReceiveSetup1;
		private WzPageReceiveProperties wzPageReceiveProperties1;
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Constructor. Sets eventhandlers for inherited buttons and 
		/// custom events. Creates the page-collection. 
		/// </summary>
		public AdapterWizardForm()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			this.ButtonNext.Click += new System.EventHandler(buttonNext_Click);
			this.ButtonBack.Click += new System.EventHandler(buttonBack_Click);

			AddPage(wzPageWelcome1,false);
			AddPage(wzPageGeneralSetup1,false);
			AddPage(wzPageReceiveSetup1,false);
			AddPage(wzPageReceiveProperties1,false);
			AddPage(wzPageTransmitSetup1,false);
			AddPage(wzPageTransmitProperties1,false);
			AddPage(wzPageCodeProperties1,false);
			AddPage(wzPageSummary1,false);

			pageCollection.Add(wzPageWelcome1);
			pageCollection.Add(wzPageGeneralSetup1);
			pageCollection.Add(wzPageReceiveSetup1);
			pageCollection.Add(wzPageReceiveProperties1);
			pageCollection.Add(wzPageTransmitSetup1);
			pageCollection.Add(wzPageTransmitProperties1);
			pageCollection.Add(wzPageCodeProperties1);
			pageCollection.Add(wzPageSummary1);

			wzPageGeneralSetup1.addWizardResultEvent += new AddWizardResultEvent(AddWizardResult);
			wzPageReceiveSetup1.addWizardResultEvent += new AddWizardResultEvent(AddWizardResult);
			wzPageReceiveProperties1.addReceiveHandlerPropertyEvent +=new AddReceiveHandlerPropertyEvent(AddReceiveHandlerProperties);
			wzPageReceiveProperties1.addReceiveEndpointPropertyEvent +=new AddReceiveEndpointPropertyEvent(AddReceiveEndpointProperties);

			wzPageTransmitSetup1.addWizardResultEvent += new AddWizardResultEvent(AddWizardResult);
			wzPageTransmitProperties1.addTransmitHandlerPropertyEvent +=new AddTransmitHandlerPropertyEvent(AddTransmitHandlerProperties);
			wzPageTransmitProperties1.addTransmitEndpointPropertyEvent +=new AddTransmitEndpointPropertyEvent(AddTransmitEndpointProperties);

			wzPageCodeProperties1.addWizardResultEvent += new AddWizardResultEvent(AddWizardResult);

			ButtonHelp.Enabled = false;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void buttonNext_Click(object sender, System.EventArgs e)
		{
			try
			{
				PageEventArgs e2 = new PageEventArgs((WizardPage)pageCollection[pageCount],PageEventButton.Next);
				pageCount = AdjustPageCount(pageCount,true);
				if (((IWizardControlInterface)pageCollection[pageCount]).NeedSummary)
				{
					((WzPageSummary)pageCollection[pageCount]).Summary = CreateSummary();
				}
				SetCurrentPage((WizardPage)pageCollection[pageCount],e2);
				ButtonNext.Enabled = ((IWizardControlInterface)pageCollection[pageCount]).NextButtonEnabled;
			}
			catch(Exception exc)
			{
				MessageBox.Show(this,exc.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		private void buttonBack_Click(object sender, System.EventArgs e)
		{
			try
			{
				PageEventArgs e2 = new PageEventArgs((WizardPage)pageCollection[pageCount],PageEventButton.Back);
				pageCount = AdjustPageCount(pageCount,false);
				SetCurrentPage((WizardPage)pageCollection[pageCount],e2);
			}
			catch(Exception exc)
			{
				MessageBox.Show(this,exc.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// Creates a summary based on the properties assembled by the wizard. To
		/// be shown on the endpage of the wizard.
		/// </summary>
		/// <returns></returns>
		private string CreateSummary()
		{
			string Summary = 
				"The adapter wizard will create the following projects:" + Environment.NewLine + Environment.NewLine;
			Summary += "- A design time project for adapter management" + Environment.NewLine;
			if (wizardResults[WizardValues.AdapterIsReceiveAdapter].ToString() == "True")
			{
				string Hosting = null;
				string Direction = null;
				if (wizardResults[WizardValues.ReceiveAdapterIsIsolated].ToString() == "True")
					Hosting  = "- An isolated ";
				else
					Hosting = "- A regular ";

				if (wizardResults[WizardValues.ReceiveAdapterSupportsReqRsp].ToString() == "True")
					Direction  = "request-response receive";
				else
					Direction = "receive";

				Summary += Hosting + Direction + " adapter project" + Environment.NewLine;
			}
			if (wizardResults[WizardValues.AdapterIsTransmitAdapter].ToString() == "True")
			{
				string BatchSupport = null;
				string Direction = null;
				if (wizardResults[WizardValues.TransmitAdapterHasExtendedBatchSupport].ToString() == "True")
					BatchSupport  = "support for batch transmissions";
				else
					BatchSupport = "no support for batch transmissions";
				
				if (wizardResults[WizardValues.TransmitAdapterSupportsSolRsp].ToString() == "True")
					Direction  = "- A solicit-response transmit";
				else
					Direction = "- A transmit";

				Summary += Direction + " adapter project with " + BatchSupport + Environment.NewLine;
			}
			return Summary;
		}
		
		/// <summary>
		/// Will have to do some page counting and skipping here since the user
		/// has the possibility to skip pages by choosing send or receive
		/// </summary>
		/// <param name="pageCount"></param>
		/// <param name="countingUp"></param>
		private int AdjustPageCount(int pageCount,bool countingUp)
		{
			if (pageCount == 0)//At the welcome page
			{
				pageCount++;
				return pageCount;
			}

			bool Receive = (wizardResults[WizardValues.AdapterIsReceiveAdapter].ToString() == "True");
			bool Transmit = (wizardResults[WizardValues.AdapterIsTransmitAdapter].ToString() == "True");

			if (Receive == Transmit)//same values is default
			{
				if (countingUp)
					pageCount++;
				else
					pageCount--;
			}
			if (Receive && !Transmit)
			{
				if (countingUp)
				{
					if (pageCount == (int)Pages.wzPageReceiveProperties1)
						pageCount += 3;//skip transmit on next
					else pageCount++;
				}
				else
				{
					if (pageCount == (int)Pages.wzPageCodeProperties1)
						pageCount -= 3;//skip transmit on back
					else
						pageCount--;
				}
			}
			if (!Receive && Transmit)
			{
				if (countingUp)
				{
					if (pageCount == (int)Pages.wzPageGeneralSetup1)
						pageCount += 3;//skip receive on next
					else pageCount++;
				}
				else
				{
					if (pageCount == (int)Pages.wzPageTransmitSetup1)
						pageCount -= 3;//skip receive on back
					else
						pageCount--;
				}
			}
			return pageCount;
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AdapterWizardForm));
			this.wzPageCodeProperties1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageCodeProperties();
			this.wzPageGeneralSetup1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageGeneralSetup();
			this.wzPageSummary1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageSummary();
			this.wzPageTransmitSetup1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageTransmitSetup();
			this.wzPageTransmitProperties1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageTransmitProperties();
			this.wzPageWelcome1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageWelcome();
			this.wzPageReceiveSetup1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageReceiveSetup();
			this.wzPageReceiveProperties1 = new MsPartners.Unisys.BizTalkAdapters.AdapterWizard.WzPageReceiveProperties();
			this.SuspendLayout();
			// 
			// wzPageCodeProperties1
			// 
			this.wzPageCodeProperties1.Back = null;
			this.wzPageCodeProperties1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageCodeProperties1.Glyph")));
			resources.ApplyResources(this.wzPageCodeProperties1, "wzPageCodeProperties1");
			this.wzPageCodeProperties1.Name = "wzPageCodeProperties1";
			this.wzPageCodeProperties1.Next = null;
			this.wzPageCodeProperties1.SubTitle = "Specify code namespace";
			this.wzPageCodeProperties1.Title = "Code properties";
			this.wzPageCodeProperties1.WizardForm = null;
			// 
			// wzPageGeneralSetup1
			// 
			this.wzPageGeneralSetup1.Back = null;
			this.wzPageGeneralSetup1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageGeneralSetup1.Glyph")));
			resources.ApplyResources(this.wzPageGeneralSetup1, "wzPageGeneralSetup1");
			this.wzPageGeneralSetup1.Name = "wzPageGeneralSetup1";
			this.wzPageGeneralSetup1.Next = null;
			this.wzPageGeneralSetup1.SubTitle = "Specify generic adapter properties";
			this.wzPageGeneralSetup1.Title = "General setup";
			this.wzPageGeneralSetup1.WizardForm = null;
			// 
			// wzPageSummary1
			// 
			this.wzPageSummary1.Back = null;
			this.wzPageSummary1.BackColor = System.Drawing.SystemColors.Window;
			this.wzPageSummary1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageSummary1.Glyph")));
			resources.ApplyResources(this.wzPageSummary1, "wzPageSummary1");
			this.wzPageSummary1.Name = "wzPageSummary1";
			this.wzPageSummary1.Next = null;
			this.wzPageSummary1.SubTitle = "";
			this.wzPageSummary1.Summary = null;
			this.wzPageSummary1.Title = "BizTalk Server Adapter Wizard";
			this.wzPageSummary1.WizardForm = null;
			// 
			// wzPageTransmitSetup1
			// 
			this.wzPageTransmitSetup1.Back = null;
			this.wzPageTransmitSetup1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageTransmitSetup1.Glyph")));
			resources.ApplyResources(this.wzPageTransmitSetup1, "wzPageTransmitSetup1");
			this.wzPageTransmitSetup1.Name = "wzPageTransmitSetup1";
			this.wzPageTransmitSetup1.Next = null;
			this.wzPageTransmitSetup1.SubTitle = "Specify transmit adapter settings";
			this.wzPageTransmitSetup1.Title = "Transmit side settings";
			this.wzPageTransmitSetup1.WizardForm = null;
			// 
			// wzPageTransmitProperties1
			// 
			this.wzPageTransmitProperties1.Back = null;
			this.wzPageTransmitProperties1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageTransmitProperties1.Glyph")));
			resources.ApplyResources(this.wzPageTransmitProperties1, "wzPageTransmitProperties1");
			this.wzPageTransmitProperties1.Name = "wzPageTransmitProperties1";
			this.wzPageTransmitProperties1.Next = null;
			this.wzPageTransmitProperties1.SubTitle = "Specify transmit adapter properties";
			this.wzPageTransmitProperties1.Title = "Transmit side properties";
			this.wzPageTransmitProperties1.WizardForm = null;
			// 
			// wzPageWelcome1
			// 
			this.wzPageWelcome1.Back = null;
			this.wzPageWelcome1.BackColor = System.Drawing.SystemColors.Window;
			this.wzPageWelcome1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageWelcome1.Glyph")));
			resources.ApplyResources(this.wzPageWelcome1, "wzPageWelcome1");
			this.wzPageWelcome1.Name = "wzPageWelcome1";
			this.wzPageWelcome1.Next = null;
			this.wzPageWelcome1.SubTitle = "This wizard will guide you through the steps needed to generate a BizTalk Server " +
				"adapter.";
			this.wzPageWelcome1.Title = "BizTalk Server Adapter Wizard";
			this.wzPageWelcome1.WizardForm = null;
			// 
			// wzPageReceiveSetup1
			// 
			this.wzPageReceiveSetup1.Back = null;
			this.wzPageReceiveSetup1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageReceiveSetup1.Glyph")));
			resources.ApplyResources(this.wzPageReceiveSetup1, "wzPageReceiveSetup1");
			this.wzPageReceiveSetup1.Name = "wzPageReceiveSetup1";
			this.wzPageReceiveSetup1.Next = null;
			this.wzPageReceiveSetup1.SubTitle = "Specify receive adapter settings";
			this.wzPageReceiveSetup1.Title = "Receive side settings";
			this.wzPageReceiveSetup1.WizardForm = null;
			// 
			// wzPageReceiveProperties1
			// 
			this.wzPageReceiveProperties1.Back = null;
			this.wzPageReceiveProperties1.Glyph = ((System.Drawing.Image)(resources.GetObject("wzPageReceiveProperties1.Glyph")));
			resources.ApplyResources(this.wzPageReceiveProperties1, "wzPageReceiveProperties1");
			this.wzPageReceiveProperties1.Name = "wzPageReceiveProperties1";
			this.wzPageReceiveProperties1.Next = null;
			this.wzPageReceiveProperties1.SubTitle = "Specify receive adapter properties";
			this.wzPageReceiveProperties1.Title = "Receive side properties";
			this.wzPageReceiveProperties1.WizardForm = null;
			// 
			// AdapterWizardForm
			// 
			resources.ApplyResources(this, "$this");
			this.Controls.Add(this.wzPageReceiveProperties1);
			this.Controls.Add(this.wzPageReceiveSetup1);
			this.Controls.Add(this.wzPageCodeProperties1);
			this.Controls.Add(this.wzPageWelcome1);
			this.Controls.Add(this.wzPageTransmitProperties1);
			this.Controls.Add(this.wzPageTransmitSetup1);
			this.Controls.Add(this.wzPageSummary1);
			this.Controls.Add(this.wzPageGeneralSetup1);
			this.Name = "AdapterWizardForm";
			this.ShowInTaskbar = false;
			this.Controls.SetChildIndex(this.wzPageGeneralSetup1, 0);
			this.Controls.SetChildIndex(this.wzPageSummary1, 0);
			this.Controls.SetChildIndex(this.wzPageTransmitSetup1, 0);
			this.Controls.SetChildIndex(this.wzPageTransmitProperties1, 0);
			this.Controls.SetChildIndex(this.wzPageWelcome1, 0);
			this.Controls.SetChildIndex(this.wzPageCodeProperties1, 0);
			this.Controls.SetChildIndex(this.wzPageReceiveSetup1, 0);
			this.Controls.SetChildIndex(this.wzPageReceiveProperties1, 0);
			this.ResumeLayout(false);

		}
		#endregion

		private void AddWizardResult(object sender,PropertyPairEvent e)
		{
			try
			{
				//Replace the propValue if it already exists
				if (wizardResults[e.Name] != null)
					wizardResults.Remove(e.Name);
				wizardResults.Add(e.Name,e.Value);
			}
			catch(Exception err)
			{
				MessageBox.Show(this,err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		private void AddProperty(List<AdapterProperty> propertyArray,PropertyPairEvent e)
		{
			if (e.RemoveItem)
			{
				foreach (AdapterProperty ap in propertyArray)
				{
					if (ap.Name == e.Name)
					{
						propertyArray.Remove(ap);
						return;
					}
				}
			}
			else
			{
				//Replace the propValue if it already exists
				foreach (AdapterProperty ap in propertyArray)
				{
					if (ap.Name == e.Name)
						propertyArray.Remove(ap);
				}
				propertyArray.Add((AdapterProperty)e.Value);
			}
		}

		private void AddTransmitHandlerProperties(object sender, PropertyPairEvent e)
		{
			try
			{
				AddProperty(TransmitHandlerProperties,e);
			}
			catch(Exception err)
			{
				MessageBox.Show(this,err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		private void AddTransmitEndpointProperties(object sender, PropertyPairEvent e)
		{
			try
			{
				AddProperty(TransmitEndpointProperties,e);
			}
			catch(Exception err)
			{
				MessageBox.Show(this,err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		private void AddReceiveHandlerProperties(object sender, PropertyPairEvent e)
		{
			try
			{
				AddProperty(ReceiveHandlerProperties,e);
			}
			catch(Exception err)
			{
				MessageBox.Show(this,err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		private void AddReceiveEndpointProperties(object sender, PropertyPairEvent e)
		{
			try
			{
				AddProperty(ReceiveEndpointProperties,e);
			}
			catch(Exception err)
			{
				MessageBox.Show(this,err.Message);
				Trace.WriteLine(err.Message + Environment.NewLine + err.StackTrace);
			}
		}

		public List<AdapterProperty> ReceiveHandlerProperties
		{
			get { return this.receiveHandlerProperties; }
		}

		public Hashtable WizardResults
		{
			get { return this.wizardResults; }
		}

		public List<AdapterProperty> TransmitEndpointProperties
		{
			get { return this.transmitEndpointProperties; }
		}

		public List<AdapterProperty> ReceiveEndpointProperties
		{
			get { return this.receiveEndpointProperties; }
		}

		public List<AdapterProperty> TransmitHandlerProperties
		{
			get { return this.transmitHandlerProperties; }
		}
	}
}

